package neu.soft.facade;

import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.FinancialVoucher;
import neu.soft.entity.Invoice;
import neu.soft.entity.PayableOrder;
import neu.soft.service.FinancialService;
import neu.soft.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
@Slf4j
public class PurchasePayHandler {

    @Autowired
    private FinancialService financialService;

    @Autowired
    private ReceiptFacade receiptFacade;

    @Autowired
    InvoiceService invoiceService;

    //前端点击应付单支付，调用controller，调用该方法进行结算
    public void payPurchaseOrder(PayableOrder payableOrder) {
        //得到购货单id
        Integer financialOrderId = payableOrder.getFinancialOrderId();
        //得到发票id
        Long invoiceId = invoiceService.findInvoiceByOrderId(String.valueOf(financialOrderId));
        //得到发票
        Invoice invoice = invoiceService.findById(invoiceId);
        //调用核算门面的方法，得到生成财务凭证
        FinancialVoucher financialVoucher = receiptFacade.supplyAccountingHandle(payableOrder, invoice);//生成财务凭证
        //得到核算的金额
        BigDecimal total = financialVoucher.getPrice();
        //结账支付
        financialService.pay(total);
        //更新发票类型

    }

}
